package com.keyancompany.zhongbaopingtai.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.MessageModel;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by firefox on 2016/6/15.
 */
public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MessageModel> modelList;
    private IChateMessageShotTypeListener shotTypeListener;

    public void setShotTypeListener(IChateMessageShotTypeListener shotTypeListener) {
        this.shotTypeListener = shotTypeListener;
    }

    public ChatMessageAdapter(List<MessageModel> list) {
        modelList = list;
    }


    /**
     * 这是一个根据不同的条目返回不同的类型的布局的方法
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return modelList.get(position).getFromUserName().equals(UserAPI.getCurrentUser().getUsername()) ? 1 : 3;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 1) {
            //发送者文本
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_text_right, parent, false);
            return new SendTxtVIewHolder(view);
        } else if (viewType == 2) {
            //发送者图片
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_image_right, parent, false);
            return new SendImageVIewHolder(view);
        } else if (viewType == 3) {
            //接受者文本
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_text_left, parent, false);
            return new FromTxtVIewHolder(view);
        } else if (viewType == 4) {
            //接受者图片
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_image_right, parent, false);
            return new FromImageVIewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final MessageModel model = modelList.get(position);
        String currentName = UserAPI.getCurrentUser().getUsername();
        if (getItemViewType(position) == 1) { //发送者文本 right
            SendTxtVIewHolder sendHold = (SendTxtVIewHolder) holder;
            sendHold.tv_item_send_txt.setText(model.getMessage());
            if (!currentName.equals(model.getFromUserName())) {
                ImageLoader.getInstance().displayImage(model.getFromUserName(), sendHold.send_person_avator);
            }

            if (model.getShotType() != 0) {
                sendHold.shotParent.setVisibility(View.VISIBLE);
            } else
                sendHold.shotParent.setVisibility(View.GONE);

            sendHold.agree.setOnClickListener(null);
            sendHold.disAgree.setOnClickListener(null);
        } else if (getItemViewType(position) == 3) { //接收文本  left
            FromTxtVIewHolder fromHold = (FromTxtVIewHolder) holder;
            fromHold.tv_item_from_txt.setText(model.getMessage());
            if (!currentName.equals(model.getFromUserName())) {
                ImageLoader.getInstance().displayImage(model.getFromUserName(), fromHold.from_person_avator);
            }

            if (model.getShotType() != 0) {
                fromHold.shotParent.setVisibility(View.VISIBLE);
            } else
                fromHold.shotParent.setVisibility(View.GONE);

            fromHold.agree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (shotTypeListener != null)
                        shotTypeListener.shotTypeClickListener(position, model, 1);
                }
            });
            fromHold.disAgree.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (shotTypeListener != null)
                        shotTypeListener.shotTypeClickListener(position, model, 2);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return modelList.size();
    }

    /**
     * 以下四个内部类为定义的不同的holder
     */
    class SendTxtVIewHolder extends RecyclerView.ViewHolder {
        private TextView tv_item_send_txt;
        private TextView tv_send_msg_date;
        private ImageView send_person_avator;

        private LinearLayout shotParent;
        private Button agree;
        private Button disAgree;

        public SendTxtVIewHolder(View view) {
            super(view);
            tv_item_send_txt = (TextView) view.findViewById(R.id.tv_item_send_txt);
            tv_send_msg_date = (TextView) view.findViewById(R.id.tv_send_msg_date);
            send_person_avator = (ImageView) view.findViewById(R.id.iv_item_send_avatr);

            shotParent = (LinearLayout) view.findViewById(R.id.action_parent);
            agree = (Button) view.findViewById(R.id.agree);
            disAgree = (Button) view.findViewById(R.id.dis_agree);
        }
    }

    class FromTxtVIewHolder extends RecyclerView.ViewHolder {
        private TextView tv_item_from_txt;
        private ImageView from_person_avator;
        private TextView tv_from_msg_date;

        private LinearLayout shotParent;
        private Button agree;
        private Button disAgree;

        public FromTxtVIewHolder(View view) {
            super(view);
            tv_item_from_txt = (TextView) view.findViewById(R.id.tv_item_from_txt);
            from_person_avator = (ImageView) view.findViewById(R.id.from_person_avator);
            tv_from_msg_date = (TextView) view.findViewById(R.id.tv_from_msg_date);

            shotParent = (LinearLayout) view.findViewById(R.id.action_parent);
            agree = (Button) view.findViewById(R.id.agree);
            disAgree = (Button) view.findViewById(R.id.dis_agree);
        }
    }

    class SendImageVIewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_item_send_image;

        public SendImageVIewHolder(View view) {
            super(view);
            iv_item_send_image = (ImageView) view.findViewById(R.id.iv_send_image);
        }
    }

    class FromImageVIewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_item_from_image;
        private ImageView from_person_avator;

        public FromImageVIewHolder(View view) {
            super(view);
            iv_item_from_image = (ImageView) view.findViewById(R.id.iv_from_image);
            from_person_avator = (ImageView) view.findViewById(R.id.from_person_avator);
        }
    }

    public interface IChateMessageShotTypeListener {
        /**
         * @param position
         * @param item
         * @param actionType 操作类型
         */
        abstract void shotTypeClickListener(int position, MessageModel item, int actionType);
    }
}
